package com.zgkxzx.算法.约瑟夫环问题;


/**
 * 创建者 zgkxzx
 * 日期   12/26/17
 * 描述
 */

public class Yephus {
    private int CIRCLE_NUMS = 10;
    private int REPORT_NUM = 5;

    private Node headNode;

    private Node currentNode;

    /**
     * 创建一个CIRCLE_NUMS的环
     */
    public void init(int circleNums, int reportNum) {
        CIRCLE_NUMS = circleNums;
        REPORT_NUM = reportNum;

        headNode = new Node(1);

        Node currentNode = headNode;
        for (int i = 2; i <= CIRCLE_NUMS; i++) {
            currentNode.next = new Node(i);
            currentNode = currentNode.next;
        }
        currentNode.next = headNode;

        //currentNode = currentNode.next;

        while (currentNode.next != currentNode) {
            for (int i = 1; i < REPORT_NUM; i++) {
                System.out.print(" -> " + currentNode.next.value);
                currentNode = currentNode.next;
            }
            System.out.println("\n出列的是 " + currentNode.next.value);
            currentNode.next = currentNode.next.next;
        }

        System.out.println("最终胜利的是 " + currentNode.value);
    }


    public void run() {
        // currentNode.next = headNode;
        while (currentNode.next != currentNode) {
            for (int i = 0; i < REPORT_NUM; i++) {
                currentNode = currentNode.next;
            }
            currentNode.next = currentNode.next.next;
            System.out.println("出列的是 " + currentNode.value);

        }

    }


    public static class Node {
        public Node next;
        public int value;

        public Node(int value) {
            this.value = value;
        }
    }
}
